import { loadEnv } from 'vite'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'; 
import createVitePlugins from './vite/plugins'
import zipPack from "vite-plugin-zip-pack" 

// https://vitejs.dev/config/ 
export default ({mode,command}: any) => {
  const env = loadEnv(mode, process.cwd(), '');
  const host = env.VUE_APP_HOST 
  return {
    lintOnSave: false,
    base: "./", 
    define: {
      'process.env': env,
      'process.platform': JSON.stringify(process.platform),
    }, 
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    plugins: [
      //vue(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [
          path.resolve(process.cwd(), 'src/icons/svg'), 
          path.resolve(process.cwd(), 'src/icons/sales')
        ],
        // 指定symbolId格式
        symbolId: 'icon-[name]'
      }),
      createVitePlugins(env, command === 'build'),
      zipPack({
        inDir: 'dist', // 输入的文件夹，即要打包的文件夹
        outDir: './', // 打包好的zip文件放置的文件夹
        outFileName: 'manage.zip', // 打包好的文件名
        pathPrefix: '' // 可选，文件路径前缀
      }) 
    ],
    server: {
      port: 3003,
      proxy: {
        // '^/websocket': {
        //   ws: true,
        //   secure: false,
        //   // 目标 API 地址
        //   target: `ws${host}`,
        //   // 将主机标头的原点更改为目标URL
        //   changeOrigin: true,
        //   proxyTimeout: 10 * 60 * 1000
        // },
        '/shipping': { //api
          target: `http${host}`,
          changeOrigin: true,
          secure: false
        }, 
        '/monitor': { //monitor
          target: `http${host}`,
          changeOrigin: true,
          secure: false
        }, 
      }
    }
  }
};

